package com.ruoyi.merchant.service.impl;

import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.merchant.config.WebSocketServer;
import com.ruoyi.merchant.domain.Shop;
import com.ruoyi.merchant.service.IShopService;
import com.ruoyi.merchant.utils.AddressResolution;
import com.ruoyi.merchant.utils.RedisUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.mapper.RescueTaskMapper;
import com.ruoyi.merchant.domain.RescueTask;
import com.ruoyi.merchant.service.IRescueTaskService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 救援任务Service业务层处理
 *
 * @author zzy
 * @date 2024-09-13
 */
@Service
public class RescueTaskServiceImpl implements IRescueTaskService {
    @Autowired
    private RescueTaskMapper rescueTaskMapper;

    @Resource
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private IShopService shopService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询救援任务
     *
     * @param id 救援任务主键
     * @return 救援任务
     */
    @Override
    public RescueTask selectRescueTaskById(Long id) {
        return rescueTaskMapper.selectRescueTaskById(id);
    }

    /**
     * 查询救援任务列表
     *
     * @param rescueTask 救援任务
     * @return 救援任务
     */
    @Override
    public List<RescueTask> selectRescueTaskList(RescueTask rescueTask) {
        return rescueTaskMapper.selectRescueTaskList(rescueTask);
    }

    /**
     * 新增救援任务
     *
     * @param rescueTask 救援任务
     * @return 结果
     */
    @Override
    public int insertRescueTask(RescueTask rescueTask) {
        return rescueTaskMapper.insertRescueTask(rescueTask);
    }

    /**
     * 修改救援任务
     *
     * @param rescueTask 救援任务
     * @return 结果
     */
    @Override
    public int updateRescueTask(RescueTask rescueTask) {
        return rescueTaskMapper.updateRescueTask(rescueTask);
    }

    /**
     * 批量删除救援任务
     *
     * @param ids 需要删除的救援任务主键
     * @return 结果
     */
    @Override
    public int deleteRescueTaskByIds(Long[] ids) {
        return rescueTaskMapper.deleteRescueTaskByIds(ids);
    }

    /**
     * 删除救援任务信息
     *
     * @param id 救援任务主键
     * @return 结果
     */
    @Override
    public int deleteRescueTaskById(Long id) {
        return rescueTaskMapper.deleteRescueTaskById(id);
    }

    /**
     * 用户端应急救援业务处理
     *
     * @param rescueTask 应急救援任务对象
     * @return TODO
     */
    @Override
    @Transactional
    public int createRescueTask(RescueTask rescueTask) {
        // 1. 插入救援任务到数据库
        int num = insertRescueTask(rescueTask);
        if (num < 1) {
            throw new RuntimeException("发起救援任务失败");
        }
        // 通过地址解析工具类获取经纬度
        Map<String, Object> urlContent = AddressResolution.getURLContent(rescueTask.getPosition());
        System.out.println("lat=" + urlContent.get("lat") + ",lng=" + urlContent.get("lng"));
        // 通过redis的Geospatial地理位置类型筛选50km以内的商家,得到符合条件的商家列表
        double lng = Double.parseDouble(urlContent.get("lng").toString());
        double lat = Double.parseDouble(urlContent.get("lat").toString());
        System.out.println(lng + " " + lat);
        return num; // 返回插入的记录数
    }

    @Override
    public int handleRescueTask(RescueTask rescueTask) {
        return 0;
    }


}
